package com.lgkj.ui;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.HairStyleHomeAdapter;
import com.lgkj.bean.BarbershopBean;
import com.lgkj.ui.manager.ShopTrendsDetailsActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.TitleBuilder;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoaderInterface;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/2 0002.
 */

public class ShopDetailsActivity extends BaseActivity {
    @BindView(R.id.tv_titles)
    TextView tvTitles;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.lv_item)
    GridView lvItem;
    @BindView(R.id.sl_view)
    LinearLayout slView;
    private List<String> slideUrl;

    @BindView(R.id.sl_banner)
    Banner slBanner;
    private BarbershopBean barbershopBean;
    private HairStyleHomeAdapter adapter;
    private String shop_id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_details;
    }

    private String phone = "", vr_link = "";

    @Override
    public void initView() {
        setTitle(getIntent().getStringExtra("shop_name"));
        adapter = new HairStyleHomeAdapter(ShopDetailsActivity.this);
        lvItem.setAdapter(adapter);
        new TitleBuilder(ShopDetailsActivity.this).setRIghtText("举报", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ShopDetailsActivity.this, OptionActivity.class);
                intent.putExtra("base_title", "举报");
                startsActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        shop_id = getIntent().getStringExtra("shop_id");
        Log.i(TAG, "initData: " + shop_id);
//        if (id == null && id.length() == 0) {
//            id = CommonUtils.getShopId(ShopDetailsActivity.this);
//        }
        showDiaLog("正在获取数据");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopDetailsActivity.this));
        map.put("barbershop_id", shop_id);
        HttpUtils.postHttp(map, CommonConstant.barbenshoinfo, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    barbershopBean = mGson.fromJson(String.valueOf(jsonObject), BarbershopBean.class);
                    if (barbershopBean != null && barbershopBean.getData() != null && barbershopBean.getData().getBarbers() != null) {
//                        toastLong("" + barbershopBean.getData().getBarbers().size(), 0);
                        adapter.setData(barbershopBean.getData().getBarbers());
                    }
                    setData();
                    initbaner();
                }
                adapter.notifyDataSetChanged();
                slView.setFocusable(true);
                slView.setFocusableInTouchMode(true);
                slView.requestFocus();
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

    private void setData() {
        if (barbershopBean.getData() != null) {
            tvTitles.setText(barbershopBean.getData().getBarbershop_name());
            tvDesc.setText(barbershopBean.getData().getBarbershop_address());
            phone = barbershopBean.getData().getBarbershop_tel();
            vr_link = barbershopBean.getData().getVr_link();
        }
    }

    private void initbaner() {
        if (barbershopBean != null && barbershopBean.getData() != null && barbershopBean.getData().getBarbershop_img() != null) {
            slideUrl = new ArrayList<>();
            for (int i = 0; i < barbershopBean.getData().getBarbershop_img().size(); i++) {
                slideUrl.add(barbershopBean.getData().getBarbershop_img().get(i).getBarbershop_img_id());
            }
            if (slideUrl.size() == 1) {
                slideUrl.addAll(slideUrl);
            }
            Log.i(TAG, "initbaner: " + slideUrl.toString());
            this.slBanner.isAutoPlay(true);
            //设置轮播时间
            this.slBanner.setDelayTime(3000);
            //设置指示器位置（当banner模式中有指示器时）
            this.slBanner.setIndicatorGravity(BannerConfig.CENTER);
            this.slBanner.setImageLoader(new ImgLoader());
            this.slBanner.setImages(slideUrl);
            this.slBanner.start();

            this.slBanner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {
                    Log.i(TAG, "OnBannerClick: " + position);
                    try {
                        Intent intent = new Intent(ShopDetailsActivity.this, ImageBrowseActivity.class);
                        intent.putExtra("image_url", slideUrl.get(position));
                        startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(ShopDetailsActivity.this, slBanner, "shareNames").toBundle());
//                    startActivity(intent);
                    } catch (Exception e) {
                        Log.e(TAG, "OnBannerClick: slBanner超出数组");
                    }
                }
            });
        }
    }


    @OnClick({R.id.vr_link, R.id.phone, R.id.tv_trends})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.vr_link:
                Intent intent = new Intent();
                intent.setClass(ShopDetailsActivity.this, WebViewActivity.class);
                intent.putExtra("title", "VR_TITLE");
                intent.putExtra("url", vr_link);
                startsActivity(intent);
                break;
            case R.id.phone:
                Intent intent1 = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                Log.i(TAG, "onClick: " + phone);
                if (phone != null && phone.length() >= 7) {
                    startsActivity(intent1);
                } else {
                    toastLong("商家暂无添加电话号码", 0);
                }
                break;
            case R.id.tv_trends:
                Intent intent2 = new Intent();
                intent2.putExtra("shop_id", shop_id);
                intent2.setClass(ShopDetailsActivity.this, ShopTrendsDetailsActivity.class);
                startsActivity(intent2);
                break;
        }
    }


    private class ImgLoader implements ImageLoaderInterface {
        @Override
        public void displayImage(Context context, Object path, View imageView) {
            if (context != null) {
                CommonUtils.LoadImage(ShopDetailsActivity.this, (ImageView) imageView, ((String) path), R.mipmap.defult_banner);
            }
        }

        @Override
        public View createImageView(Context context) {
            return null;
        }
    }
}
